﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AJRavindiran.Jolt.RuneScape.Network.Login
{
    /// <summary>
    /// Represents a login stream buffer.
    /// </summary>
    public class LoginStreamBuffer
    {
        #region Methods
        /// <summary>
        /// Check and read any incoming bytes.
        /// </summary>
        public bool FillStream(Models.Characters.Character character, int forceRead)
        {
            try
            {
                if (forceRead >= 500)
                    return false;
                if (character.GetSocket().Availible < forceRead)
                    return false;

                character.GetStreamReader().ReadOffset = 0;
                character.GetSocket().Read(forceRead);
                return true;
            }
            catch (Streaming.StreamException se)
            {
                Jolt.GetLog().WriteException(se);
                throw se;
            }
        }

        /// <summary>
        /// Send the bytes in the stream's outBuffer directly to the client.
        /// </summary>
        public void DirectFlushStream(Models.Characters.Character character)
        {
            try
            {
                character.GetSocket().Write(
                    character.GetStreamWriter().OutBuffer, 0, 
                    character.GetStreamWriter().WriteOffset);
                character.GetStreamWriter().WriteOffset = 0;
            }
            catch (Streaming.StreamException se)
            {
                Jolt.GetLog().WriteException(se);
                throw se;
            }
        }
        #endregion Methods
    }
}
